//
//  DDMoneybagViewController.m
//  DingDongZP
//
//  Created by 杨政涛 on 16/10/13.
//  Copyright © 2016年 51ledian.com. All rights reserved.
//

#import "DDMoneybagViewController.h"
#import "DDMoneybagHeaderView.h"
#import "DDPayViewController.h" //充值页面
#import "DDMoneyDetailViewController.h" //账户明细页面
#import "DDShareAccountViewController.h" //共享账户

#import "DDMoneybagHomepageService.h"

#import "DDMoneybagHomepageModel.h"


@interface DDMoneybagViewController ()

@property(nonatomic,strong)NSArray *listArr;

@property(nonatomic,strong)DDMoneybagHeaderView *headView;

@property(nonatomic,strong)DDMoneybagHomepageService *moneybagHomepageService;

@property(nonatomic,strong)DDMoneybagHomepageModel *moneybagHomepage;

@end

@implementation DDMoneybagViewController


- (void)viewWillAppear:(BOOL)animated
{
    [self.rdv_tabBarController setTabBarHidden:true animated:true];
    [self setupData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"钱包";
    [self addNavigationRightBar];
    
    self.listArr = @[@[@{@"listPicName":@"chongzhi.png",@"listName":@"充值",@"rightDesc":@""}],@[@{@"listPicName":@"gongxiang.png",@"listName":@"共享账户",@"rightDesc":@"0个"},@{@"listPicName":@"fapiao.png",@"listName":@"发票",@"rightDesc":@"0个"}]];
    
    self.moneybagHomepageService = [[DDMoneybagHomepageService alloc]init];
    
    [self configureTableView];
}





- (void)addNavigationRightBar
{
    
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"明细" style:UIBarButtonItemStylePlain target:self action:@selector(lookDetail)];
    self.navigationItem.rightBarButtonItem = rightButton;
}

- (void)configureTableView
{
    self.tableView.rowHeight = 50;
    WEAKSELF
    [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.view);
        make.right.mas_equalTo(weakSelf.view);
        make.top.mas_equalTo(weakSelf.view);
        make.bottom.mas_equalTo(weakSelf.view);
    }];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.headView = [[DDMoneybagHeaderView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 156)];
    self.tableView.tableHeaderView = self.headView;
    [self addRefreshHeader];
}

#pragma mark  UITableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return self.listArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    NSArray *sectionArray = self.listArr[section];
    
    return sectionArray.count;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    meListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"moneybag"];
    if (cell == nil) {
        
        cell = [[meListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"moneybag"];
    }
    
    NSDictionary *dict = self.listArr[indexPath.section][indexPath.row];
    meListCellModel *models = [meListCellModel yy_modelWithDictionary:dict];
    cell.model = models;
    
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return TableViewSectionHeight;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    //test
    if (indexPath.row == 1 && indexPath.section == 1) {
        DDLog(@"indexPath = %ld",indexPath.row);
        
        //
        DDInvoice *inVc = [[DDInvoice alloc] init];
        [self pushViewController:inVc animated:true];
    }
    
    switch (indexPath.section) {
        case 0:
        {
            DDPayViewController *payVC = [[DDPayViewController alloc]init];
            [self.navigationController pushViewController:payVC animated:YES];
        }
            break;
            case 1:
        {
            if (indexPath.row == 0) {
             
                DDShareAccountViewController *shareAccountVC = [[DDShareAccountViewController alloc]init];
                [self.navigationController pushViewController:shareAccountVC animated:YES];
            }
        }
            break;
            
        default:
            break;
    }
}

// 查看明细
- (void)lookDetail
{
    DDMoneyDetailViewController *detailVC = [[DDMoneyDetailViewController alloc]init];
    [self.navigationController pushViewController:detailVC animated:YES];
}

// 网络请求
- (void)setupData
{
    
    WEAKSELF
    [self.moneybagHomepageService queryMoneybagHomepageSuccessBlock:^(id responseObject) {
                
        [weakSelf endRefreshHeader];
        self.moneybagHomepage = [DDMoneybagHomepageModel yy_modelWithJSON:responseObject[@"data"]];
        [self performSelectorOnMainThread:@selector(backToMainToUpdata) withObject:self waitUntilDone:YES];
        
    } failBlock:^(id responseObject, NSError *error) {
        
        [weakSelf endRefreshHeader];
        [weakSelf requestFaild];
    }];
}

- (void)backToMainToUpdata
{
    self.headView.dataModel = self.moneybagHomepage;
    
    self.listArr = @[@[@{@"listPicName":@"chongzhi.png",@"listName":@"充值",@"rightDesc":self.moneybagHomepage.remark}],@[@{@"listPicName":@"gongxiang.png",@"listName":@"共享账户",@"rightDesc":[NSString stringWithFormat:@"%@个",!self.moneybagHomepage.share_num?@"0":self.moneybagHomepage.share_num]},@{@"listPicName":@"fapiao.png",@"listName":@"发票",@"rightDesc":[NSString stringWithFormat:@"%@个",!self.moneybagHomepage.invoice_num?@"0":self.moneybagHomepage.invoice_num]}]];
    
    [self.tableView reloadData];
}

// 下拉刷新更新数据
- (void)loadNewData
{

    [self setupData];
}

- (void)viewWillDisappear:(BOOL)animated
{
    
    [self.rdv_tabBarController setTabBarHidden:false animated:true];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
